package utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class DBGetDataUtil {

    public static JSONArray getSensors(String sql, Object... params) throws Exception {
        final JSONArray resultArray = new JSONArray();
        final JSONObject result = new JSONObject();
        new DBSelect(sql, params).exec(new DBSelectInt() {

            @Override
            public boolean deal(ResultSet rs) throws SQLException {
                while (rs.next()) {
                    String r = rs.getString(1);
                    result.put("val", r.trim());
                    resultArray.add(result);
                }
                return false;
            }
        });
        return resultArray;
    }

    public static JSONObject getJsonData(String warning, String sql, String dataTable, Object... params)
            throws Exception {
        JSONObject result = new JSONObject();
        final List<Double> timeList = new ArrayList<>();
        final List<Double> dataList = new ArrayList<>();
        new DBSelect(sql, params).exec(new DBSelectInt() {

            @Override
            public boolean deal(ResultSet rs) throws SQLException {
                while (rs.next()) {
                    timeList.add((double) DateUtil.stringToDataMillisecond(rs.getString(1)));
                    dataList.add(rs.getDouble(2));
                }
                return false;
            }
        });
        result.put(Pub.TIME, timeList);
        result.put("data", dataList);
        if (Boolean.parseBoolean(warning)) {
            final LinkedList<Double> warns = new LinkedList<>();
            final String str = "select WARNING from Warning where NAME = '" + dataTable + "'";
            new DBSelect(str).exec(new DBSelectInt() {

                @Override
                public boolean deal(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        warns.add(rs.getDouble(1));
                    }
                    return false;
                }
            });
            if (warns.isEmpty()) {
                result.put("warning", "warning值查询失败");
            } else {
                result.put("warning", warns.getLast());
            }
        }
        return result;
    }

    public static JSONObject getJsonDataForMainbeam(String warning, String sql, String dataTable, Object... params)
            throws Exception {
        JSONObject result = new JSONObject();
        final List<Double> timeList = new ArrayList<>();
        final List<Double> maxList = new ArrayList<>();
        final List<Double> minList = new ArrayList<>();
        final List<Double> avgList = new ArrayList<>();
        new DBSelect(sql, params).exec(new DBSelectInt() {

            @Override
            public boolean deal(ResultSet rs) throws SQLException {
                while (rs.next()) {
                    timeList.add((double) DateUtil.stringToDataMillisecond(rs.getString(1)));
                    maxList.add(rs.getDouble(2));
                    minList.add(rs.getDouble(3));
                    avgList.add(rs.getDouble(4));
                }
                return false;
            }
        });
        result.put(Pub.TIME, timeList);
        result.put("max", maxList);
        result.put("min", minList);
        result.put("avg", avgList);
        if (Boolean.parseBoolean(warning)) {
            final LinkedList<Double> warns = new LinkedList<>();
            final String str = "select WARNING from Warning where NAME = '" + dataTable + "'";
            new DBSelect(str).exec(new DBSelectInt() {

                @Override
                public boolean deal(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        warns.add(rs.getDouble(1));
                    }
                    return false;
                }
            });
            if (warns.isEmpty()) {
                result.put("warning", "warning值查询失败");
            } else {
                result.put("warning", warns.getLast());
            }
        }
        return result;
    }

    public static JSONObject getJsonDataForMaintower(String red, String yellow, String sql, String dataTable,
                                                     Object... params) throws Exception {
        JSONObject result = new JSONObject();
        final List<Double> timeList = new ArrayList<>();
        final List<Double> maxList = new ArrayList<>();
        final List<Double> minList = new ArrayList<>();
        final List<Double> avgList = new ArrayList<>();
        final List<Double> redList = new ArrayList<>();
        final List<Double> yellowList = new ArrayList<>();
        new DBSelect(sql, params).exec(new DBSelectInt() {

            @Override
            public boolean deal(ResultSet rs) throws SQLException {
                while (rs.next()) {
                    timeList.add((double) DateUtil.stringToDataMillisecond(rs.getString(1)));
                    maxList.add(rs.getDouble(2));
                    minList.add(rs.getDouble(3));
                    avgList.add(rs.getDouble(4));
                    /*
                     * redList.add(rs.getDouble(5));
                     * yellowList.add(rs.getDouble(6));
                     */
                }
                return false;
            }
        });
        result.put(Pub.TIME, timeList);
        result.put("max", maxList);
        result.put("min", minList);
        result.put("avg", avgList);
        /*
         * result.put("red", redList); result.put("yellow", yellowList);
         */

        if (Boolean.parseBoolean(red)) {
            final LinkedList<Double> warns = new LinkedList<>();
            final String str = "select WARNING from Warning where NAME = '" + dataTable + "'";
            new DBSelect(str).exec(new DBSelectInt() {

                @Override
                public boolean deal(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        warns.add(rs.getDouble(1));
                    }
                    return false;
                }
            });
            if (warns.isEmpty()) {
                result.put("redWarning", "warning值查询失败");
            } else {
                result.put("redWarning", warns.getLast());
            }
        }

        if (Boolean.parseBoolean(yellow)) {
            final LinkedList<Double> warns = new LinkedList<>();
            final String str = "select WARNING from Warning where NAME = '" + dataTable + "'";
            new DBSelect(str).exec(new DBSelectInt() {

                @Override
                public boolean deal(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        warns.add(rs.getDouble(1));
                    }
                    return false;
                }
            });
            if (warns.isEmpty()) {
                result.put("yellowWarning", "warning值查询失败");
            } else {
                result.put("yellowWarning", 12);
            }
        }
        return result;
    }

    public static String getPwd(String sql, final String password) {
        final String[] pwd = new String[1];
        new DBSelect(sql).exec(new DBSelectInt() {
            @Override
            public boolean deal(ResultSet rs) throws SQLException {
                while (rs.next()) {
                    pwd[0] = rs.getString(1);
                }
                return false;
            }
        });
        return pwd[0];
    }

}
